import { FindAllOrderDto } from './dto/findAll-order.dto';
import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Query,
  Request
} from '@nestjs/common';
import { OrderService } from './order.service';
import { CreateOrderDto } from './dto/create-order.dto';
import { UpdateOrderDto } from './dto/update-order.dto';
import { ApiResult } from 'src/config';
import { ApiOperation } from '@nestjs/swagger';

@Controller('order')
export class OrderController {
  constructor(private readonly orderService: OrderService) {}

  @ApiOperation({ summary: '创建订单' })
  @Post()
  async create(@Body() createOrderDto: CreateOrderDto): Promise<ApiResult> {
    const result = await this.orderService.create(createOrderDto);
    return ApiResult.SUCCESS(result, 0, '创建成功!');
  }

  @ApiOperation({ summary: '获取订单信息' })
  @Get()
  async findAll(@Query() findAllOrderDto: FindAllOrderDto): Promise<ApiResult> {
    const result = await this.orderService.findAll(findAllOrderDto);
    return ApiResult.SUCCESS(result, 0, '获取成功！');
  }

  @ApiOperation({ summary: '根据id获取订单信息' })
  @Get(':id')
  async findOne(
    @Request() request,
    @Param('id') id?: string): Promise<ApiResult> {
    const { user_id } = request.user;
    const result = await this.orderService.findOne(+id ?? user_id);
    return ApiResult.SUCCESS(result ?? [], 0, '获取成功！');
  }

  @ApiOperation({ summary: '根据id修改订单信息' })
  @Patch(':id')
  async update(
    @Param('id') id: string,
    @Body() updateOrderDto: UpdateOrderDto,
  ): Promise<ApiResult> {
    const result = await this.orderService.update(+id, updateOrderDto);
    return ApiResult.SUCCESS(result, 0, '修改成功!');
  }

  @ApiOperation({ summary: '根据id删除订单信息' })
  @Delete(':id')
  async remove(@Param('id') id: string): Promise<ApiResult> {
    const result = await this.orderService.remove(+id);
    if (result.affected >= 1) {
      return ApiResult.SUCCESS([], 0, '删除成功!');
    } else {
      return ApiResult.ERROR(-1, '删除失败!');
    }
  }
}
